home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 04 Mommersteeg / Penny / SelfAdjustingPredictor.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-24  |  2.2 KB  |  57 lines

  1. //----------------------------------------------------------------------------------------------
  2. // Sequential Prediction Demo: The positioning pattern
  3. // 
  4. // Author:  Fri Mommersteeg
  5. // Date:    10-09-2001
  6. // File:    SelfAdjustingPredictor.cpp
  7. //----------------------------------------------------------------------------------------------
  8.  
  9. //----------------------------------------------------------------------------------------------
  10. // Include files
  11. //----------------------------------------------------------------------------------------------
  12.  
  13. #include "stdafx.h"
  14. #include "selfadjustingpredictor.h"
  15.  
  16. //----------------------------------------------------------------------------------------------
  17. // Constants
  18. //----------------------------------------------------------------------------------------------
  19.  
  20. const float g_Punishment = 1.1f;
  21. const float g_Reward = 0.9f;
  22.  
  23. //----------------------------------------------------------------------------------------------
  24. // Update(): overloaded to evaluate previous prediction
  25. //----------------------------------------------------------------------------------------------
  26.  
  27. void CSelfAdjustingPredictor::Update(int NextElement) {
  28.     
  29.     // did the predictor think the estimation was correct?
  30.     if (m_EstimatedCorrect) {
  31.         // check if the predictor was wrong
  32.         if (NextElement != m_nPrediction) {
  33.             // punish the predictor
  34.             m_fMinPerformance *= g_Punishment;
  35.         } else {
  36.             // reward the predictor if the prediction was correct
  37.             if (NextElement==m_nPrediction) {
  38.                 m_fMinPerformance *= g_Reward;
  39.             }
  40.         }
  41.     }
  42.     
  43.  
  44.     // add next element to the predictor
  45.     CImprovedPredictor::Update(NextElement);
  46. }
  47.  
  48. //----------------------------------------------------------------------------------------------
  49. // GetPrediction(): overloaded to catch the correctness estimation
  50. //----------------------------------------------------------------------------------------------
  51.  
  52. bool CSelfAdjustingPredictor::GetPrediction(int &Prediction) {
  53.  
  54.     // catch the correctness estimation (for evaluation during next update)
  55.     m_EstimatedCorrect = CImprovedPredictor::GetPrediction(Prediction);
  56.     return m_EstimatedCorrect;
  57. }